﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Controler;
using Waiter.Common;

namespace Waiter.View
{
    public partial class FrmOrderCoupon : DevComponents.DotNetBar.Office2007Form
    {
        public FrmOrderCoupon()
        {
            InitializeComponent();
        }
        public FrmOrderCoupon(string tableCode)
        {
            InitializeComponent();
            this.tableCode = tableCode;
        }

        bool dataChangedFlag = CommonConstants.FLAG_OFF;

        public bool DataChangedFlag
        {
            get { return dataChangedFlag; }
        }
        string tableCode = "";
        bool loadingFlag = CommonConstants.FLAG_ON;
        private void btnCancel_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnCancel_Click", "", "");
            this.Close();
        }

        private void FrmOrderCoupon_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.FrmOrderCoupon_Load", "", "");
            this.fsLoadCMBGroup();
            this.fsLoadDgrMenu();
            this.fsLoadDgrOrderMenu();
            this.loadingFlag = CommonConstants.FLAG_OFF;
        }

        public void fsLoadCMBGroup()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.fsLoadCMBGroup", "", "");
            MasterCtrl masterCtrl = new MasterCtrl();
            ComboboxItem item = new ComboboxItem();
            item.Text = "Tất cả";
            item.Value = "0";
            masterCtrl.fsLoadCmbGroupList(this.cmbGroup, CommonConstants.NHOM_MON, item);
            CommonCtrl.fsCmbSetSelectedIndex(this.cmbGroup, 0);
        }

        public void fsLoadDgrMenu()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.fsLoadDgrMenu", "", "");
            MenuCtrl menuCtrl = new MenuCtrl();
            List<MENU> menuList = null;
            string selectGroup = CommonCtrl.fsCmbGetSelectedValue(this.cmbGroup);
            if (selectGroup == "0")
            {
                menuList = menuCtrl.fsGetMenuList((int)MenuStatus.AVAILABLE);
            }
            else
            {
                menuList = menuCtrl.fsGetMenuListByGroup(selectGroup, (int)MenuStatus.AVAILABLE);
            }

            if (menuList == null || menuList.Count == 0)
            {
                return;
            }

            this.dgrMenu.Rows.Add(menuList.Count);
            for (int i = 0; i < menuList.Count; i++)
            {
                this.dgrMenu.Rows[i].Cells["colSTT"].Value = i + 1;
                this.dgrMenu.Rows[i].Cells["colName"].Value = menuList[i].DRINKS_NAME;
                this.dgrMenu.Rows[i].Cells["colCode"].Value = menuList[i].CODE;
            }
        }

        public void fsLoadDgrOrderMenu()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.fsLoadDgrOrderMenu", "", "");
            OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            List<ORDER_COUPON_D> orderCouponList = orderCouponCtrl.fsGetCurrentOrderCouponList(tableCode);
            if (orderCouponList == null || orderCouponList.Count == 0)
            {
                return;
            }

            this.dgrOrderMenu.Rows.Add(orderCouponList.Count);
            int count = 0;
            for (int i = 0; i < orderCouponList.Count; i++)
            {
                dgrOrderMenu.Rows[i].Cells["colOrderName"].Value = orderCouponList[i].MENU.DRINKS_NAME;
                dgrOrderMenu.Rows[i].Cells["colOrderCount"].Value = orderCouponList[i].COUNT;
                dgrOrderMenu.Rows[i].Cells["colOrderCode"].Value = orderCouponList[i].MENU_CODE;
                count += (int)orderCouponList[i].COUNT;
            }

            this.progressBarX1.Text = count.ToString();
            this.progressBarX1.Value = count;
        }

        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.cmbGroup_SelectedIndexChanged", "", "");
            if (this.loadingFlag == CommonConstants.FLAG_ON)
            {
                return;
            }

            dgrMenu.Rows.Clear();
            fsLoadDgrMenu();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnAdd_Click", "", "");
            if (this.dgrMenu.Rows.Count == 0)
            {
                return;
            }

            int idx = this.dgrMenu.CurrentRow.Index;
            AddDgrOrderMenu(this.dgrMenu.Rows[idx].Cells["colCode"].Value.ToString(),
                this.dgrMenu.Rows[idx].Cells["colName"].Value.ToString());

            int total = Convert.ToInt32(this.progressBarX1.Text);
            this.progressBarX1.Text = (total + 1).ToString();
            this.progressBarX1.Value = total + 1;
        }

        private void AddDgrOrderMenu(string menuCode, string menuName)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.AddDgrOrderMenu", "", "");
            int idx = -1;
            int count = 0;
            for (int i = 0; i < this.dgrOrderMenu.Rows.Count; i++)
            {
                if (this.dgrOrderMenu.Rows[i].Cells["colOrderCode"].Value.ToString() == menuCode)
                {
                    count = Convert.ToInt32(this.dgrOrderMenu.Rows[i].Cells["colOrderCount"].Value);
                    idx = i;
                    break;
                }
            }

            if (idx == -1)
            {
                this.dgrOrderMenu.Rows.Add(1);
                idx = this.dgrOrderMenu.Rows.Count - 1;
                this.dgrOrderMenu.Rows[idx].Cells["colOrderCode"].Value = menuCode;
                this.dgrOrderMenu.Rows[idx].Cells["colOrderName"].Value = menuName;
            }

            this.dgrOrderMenu.Rows[idx].Cells["colOrderCount"].Value = count + 1;
            CommonCtrl.fsDgrSetSelectedRow(this.dgrOrderMenu, idx, 1);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnRemove_Click", "", "");
            if (this.dgrOrderMenu.Rows.Count == 0)
            {
                return;
            }

            int idx = this.dgrOrderMenu.CurrentRow.Index;
            int count = Convert.ToInt32(this.dgrOrderMenu.CurrentRow.Cells["colOrderCount"].Value);
            if (count == 1)
            {
                this.dgrOrderMenu.Rows.RemoveAt(idx);
            }
            else
            {
                this.dgrOrderMenu.CurrentRow.Cells["colOrderCount"].Value = count - 1;
            }

            int total = Convert.ToInt32(this.progressBarX1.Text);
            this.progressBarX1.Text = (total - 1).ToString();
            this.progressBarX1.Value = total - 1;
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnRemoveAll_Click", "", "");
            if (this.dgrOrderMenu.Rows.Count == 0)
            {
                return;
            }
            this.dgrOrderMenu.Rows.RemoveAt(this.dgrOrderMenu.CurrentRow.Index);
            int count = 0;
            for (int i = 0; i < this.dgrOrderMenu.Rows.Count; i++)
            {
                count += Convert.ToInt32(this.dgrOrderMenu.Rows[i].Cells["colOrderCount"].Value);
            }
            this.progressBarX1.Text = count.ToString();
            this.progressBarX1.Value = count;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnSave_Click", "", "");
            TableCtrl tableCtrl = new TableCtrl();
            ResultStatus resultStatus;

            // xoa thong tin goi mon da ton tai
            OrderCouponCtrl orderCouponCtrl = new OrderCouponCtrl();
            resultStatus = orderCouponCtrl.fsHRemoveAllCurrentOrderCouponDetail(this.tableCode);

            if (this.dgrOrderMenu.Rows.Count == 0)
            {
                resultStatus = orderCouponCtrl.fsHRemoveCurrentOrderCoupon(this.tableCode);
                resultStatus = tableCtrl.fsUpdateTableStatus(this.tableCode, (int)TableStatus.EMPTY);

                // dong form
                resultStatus = tableCtrl.fsUpdateTableStatus(this.tableCode, (int)TableStatus.EMPTY);
                this.dataChangedFlag = CommonConstants.FLAG_ON;
                this.Close();
                return;
            }

            // tao phieu goi mon
            ORDER_COUPON orderCoupon = orderCouponCtrl.fsGetCurrentOrderCoupon(this.tableCode);
            if (orderCoupon == null)
            {
                orderCoupon = new ORDER_COUPON();
                orderCoupon.CODE = CommonUtils.fsCreateNewCode();
                orderCoupon.TABLE_CODE = this.tableCode;
                orderCoupon.STATUS = (int)OrderCouponStatus.NOT_CHARGED;

                orderCoupon.OPID = CommonUtils.fsGetOPID();
                orderCoupon.DEL_FLG = CommonConstants.FLAG_OFF;
                orderCoupon.ADD_DATE = DateTime.Now;
                orderCoupon.UPD_DATE = DateTime.Now;

                resultStatus = orderCouponCtrl.fsInsertOrderCoupon(orderCoupon);
            }

            // tao chi tiet phieu goi mon
            MenuCtrl menuCtrl = new MenuCtrl();
            string areaCode = tableCtrl.fsFindTableByCode(this.tableCode).AREA_CODE;
            List<ORDER_COUPON_D> orderCouponDetails = new List<ORDER_COUPON_D>();
            for (int i = 0; i < this.dgrOrderMenu.Rows.Count; i++)
            {
                ORDER_COUPON_D item = new ORDER_COUPON_D();
                item.COUPON_CODE = orderCoupon.CODE;
                item.MENU_CODE = this.dgrOrderMenu.Rows[i].Cells["colOrderCode"].Value.ToString();
                item.COUNT = Convert.ToInt32(this.dgrOrderMenu.Rows[i].Cells["colOrderCount"].Value.ToString());
                item.STATUS = (int)OrderCouponDStatus.NOT_PREPARED;
                item.PRICE = menuCtrl.fsGetSalesPrice(item.MENU_CODE, areaCode);

                item.OPID = CommonUtils.fsGetOPID();
                item.DEL_FLG = CommonConstants.FLAG_OFF;
                item.ADD_DATE = DateTime.Now;
                item.UPD_DATE = DateTime.Now;

                orderCouponDetails.Add(item);
            }
            resultStatus = orderCouponCtrl.fsInsertOrderCouponDetail(orderCouponDetails);

            // dong form
            resultStatus = tableCtrl.fsUpdateTableStatus(this.tableCode, (int)TableStatus.BUSY);
            this.dataChangedFlag = CommonConstants.FLAG_ON;
            this.Close();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnUp_Click", "", "");
            if (this.dgrOrderMenu.Rows.Count == 0)
            {
                return;
            }
            int num = Convert.ToInt32(this.dgrOrderMenu.CurrentRow.Cells["colOrderCount"].Value);
            this.dgrOrderMenu.CurrentRow.Cells["colOrderCount"].Value = num + 1;

            int total = Convert.ToInt32(this.progressBarX1.Text);
            this.progressBarX1.Text = (total + 1).ToString();
            this.progressBarX1.Value = total + 1;
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.btnDown_Click", "", "");
            if (this.dgrOrderMenu.Rows.Count == 0)
            {
                return;
            }

            int idx = this.dgrOrderMenu.CurrentRow.Index;
            int count = Convert.ToInt32(this.dgrOrderMenu.CurrentRow.Cells["colOrderCount"].Value);
            if (count == 1)
            {
                this.dgrOrderMenu.Rows.RemoveAt(idx);
            }
            else
            {
                this.dgrOrderMenu.CurrentRow.Cells["colOrderCount"].Value = count - 1;
            }

            int total = Convert.ToInt32(this.progressBarX1.Text);
            this.progressBarX1.Text = (total - 1).ToString();
            this.progressBarX1.Value = total - 1;
        }

        private void dgrMenu_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.dgrMenu_CellDoubleClick", "", "");
            this.btnAdd_Click(null, null);
        }

        private void dgrOrderMenu_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmOrderCoupon.dgrOrderMenu_CellDoubleClick", "", "");
            this.btnRemove_Click(null, null);
        }
    }
}